<p:config xmlns:p="http://www.orbeon.com/oxf/pipeline" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:oxf="http://www.orbeon.com/oxf/processors" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xxforms="http://orbeon.org/oxf/xml/xforms" xmlns:xdb="http://orbeon.org/oxf/xml/xmldb" xmlns:xu="http://www.xmldb.org/xupdate">

	<p:processor name="oxf:pipeline">
		<p:input name="config" href="../session.xpl"/>
		<p:output name="data" id="session"/>
	</p:processor>
	
	<!--Remove member from group and delete group if no more members left-->
	<p:processor name="oxf:xslt">
		<p:input name="data" href="#session"/>
		<p:output name="data" id="query"/>
		<p:input name="config">
			<xdb:query collection="/db/portal" xsl:version="2.0">
				<xsl:text>xquery version "1.0";</xsl:text>
				let $gr := /groups/group[@id='<xsl:value-of select="/session/groups/@current"/>'] return (
				update delete $gr/members/member[@id='<xsl:value-of select="/session/username"/>'],
				if (count($gr/members/member) = 0) then update delete $gr else ())
			</xdb:query>
		</p:input>
	</p:processor>

	<p:processor name="oxf:xmldb-query">
		<p:input name="datasource" href="/config/datasource.xml"/>
		<p:input name="query" href="#query"/>
		<p:output name="data" id="result"/>
	</p:processor>
	
	<p:processor name="oxf:null-serializer">
		<p:input name="data" href="#result"/>
	</p:processor>
		
	<!--Partial Session rewrite-->
	<p:processor name="oxf:xupdate">
		<p:input name="data" href="#session"/>
		<p:output name="data" id="mod_session"/>
		<p:input name="config">
			<xu:modifications xmlns:xu="http://www.xmldb.org/xupdate">
				<xu:update select="/session/groups">
					<xu:attribute name="current">gr_0</xu:attribute>
				</xu:update>
			</xu:modifications>
		</p:input>
	</p:processor>
	<p:processor name="oxf:scope-serializer">
		<p:input name="config">
			<config>
				<key>session</key>
				<scope>session</scope>
				<session-scope>application</session-scope>
			</config>
		</p:input>
		<p:input name="data" href="#mod_session"/>
	</p:processor>

	<p:processor name="oxf:redirect">
		<p:input name="data">
			<redirect-url>
				<path-info>/portal/main/</path-info>
			</redirect-url>
		</p:input>
	</p:processor>
</p:config>